package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class worklogin
 */
@WebServlet("/worklogin")
public class worklogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public worklogin() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String piccode=(String)request.getSession().getAttribute("piccode");
		System.out.println(piccode);
		String checkcode=request.getParameter("checkcode");
		System.out.println(request.getContextPath());
		//如果验证码不正确或者为空那么就直接结束
		if(!checkcode.equalsIgnoreCase(piccode)) {
			String result="checkcode";
			request.getRequestDispatcher("login.jsp?result=" + result + "").forward(request, response);
			return;
		}
		String adminid = request.getParameter("username");
		String adminps = request.getParameter("password");

		String find = "select * from user where username='" + adminid + "'";
		System.out.println(find);
		String fpsw = null;
		String aname = null;
		String role = null;
		Jdbc j = new Jdbc();

		try {
			j.Jdbc_Q2(find);
			j.rs.next();
			fpsw = j.rs.getString("password");
			aname = j.rs.getString("username");
			role = j.rs.getString("role");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (fpsw == null) {
			String result = "empty";
			request.getRequestDispatcher("login.jsp?result=" + result + "").forward(request, response);
			return;
		}
		if (fpsw.equals(adminps)) {
			request.getSession().setAttribute("password", fpsw);
			request.getSession().setAttribute("username", aname);
			request.getSession().setAttribute("role", role);
			request.getRequestDispatcher("HomePage.jsp").forward(request, response);
		}

		else if (aname.equals("")) {
			String result = "empty";
			request.getRequestDispatcher("login.jsp?result=" + result + "").forward(request, response);
		} else {
			String result = "failed";
			request.getRequestDispatcher("login.jsp?result=" + result + "").forward(request, response);

		}
	}

}
